/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.expert.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.common.utils.StringUtils;
import com.chlitina.store.modules.expert.dao.ProductDao;
import com.chlitina.store.modules.expert.entity.Product;
import com.chlitina.store.modules.expert.entity.ProductCategory;
import com.chlitina.store.modules.sys.entity.Store;
import com.chlitina.store.modules.sys.utils.StoreUtils;

/**
 * 产品Service
 * @author lvbo
 * @version 2013-06-04
 */
@Component
@Transactional(readOnly = true)
public class ProductService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(ProductService.class);
	
	@Autowired
	private ProductDao productDao;
	
	public Product get(Long id) {
		return productDao.findOne(id);
	}
	
	public Page<Product> find(Page<Product> page, Long category_id) {
		DetachedCriteria dc = productDao.createDetachedCriteria();
		dc.add(Restrictions.eq("productCategory.id", category_id));
		dc.add(Restrictions.eq("status", Product.FLAG_ENABLE));
		dc.addOrder(Order.asc("id"));
		return productDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public List<Product> findAllByName(String searchName) {
		DetachedCriteria dc = productDao.createDetachedCriteria();
		dc.add(Restrictions.eq("status", Product.FLAG_ENABLE));
		if (StringUtils.isNotEmpty(searchName)){
			dc.add(Restrictions.like("name", "%"+searchName+"%"));
		}
		dc.addOrder(Order.asc("id"));
		return productDao.find(dc);
	}
	
	@Transactional(readOnly = false)
	public List<Product> findAllAvailable(int status) {
		return (List<Product>) productDao.findAllAvailable(status);
	}
}
